<?php
	
	require '../../config.php';	
	$result = mysql_query("select p.seleccionar,p.insertar,p.eliminar,p.abrir,p.imprimir,p.actualizar from permisos p,modulos m where p.idmodulo = m.idmodulos and p.idrol= '". $_SESSION['rol'] ."' and m.nombre='Ventas'",$conexion);
	if(mysql_num_rows($result)>0)
	{
		$rowp = mysql_fetch_assoc($result);
		if($rowp['abrir']!= 1)
		{
			echo "<legend>No tiene permiso</legend>";
			exit();
		}
	}
	$_codigo = $_REQUEST['id'];
	if($_codigo==2)//potenciales
	{
		$_query = "select * from  clientesventas where tipo = '2' and activo=1 order by idclientesventas desc ";
		echo "<legend>Lista de Clientes Potenciales</legend>";
	}
	if($_codigo==1)//prospectos
	{
		$_query = "select * from  clientesventas where tipo = '1' and activo=1  order by idclientesventas desc ";
		echo "<legend>Lista de Clientes Prospectos</legend>";
		}
	if($_codigo==3)//activos
	{
		$_query = "select * from  clientesventas where tipo = '3' and activo=1  order by idclientesventas desc ";
		echo "<legend>Lista de Clientes Activos</legend>";
		}
	
		$registros=mysql_query($_query) or die("Problemas en el select:".mysql_error());
		if (mysql_num_rows($registros) == 0)
		{
			echo "<legend>Sin Registros</legend>";
		}
		else
		{	
			
			echo   "<table class='table table-bordered'>";
		
			echo   "<tr>";
			echo   "<th>Identificador</th><th>Nombre</th><th>Direcci&oacute;n</th><th>Tel&eacute;fono</th><th>Contacto</th><th>Tel. contacto</th><th>Cargo contacto</th><th>Email contacto</th><th>Opciones</th>";
			echo   "</tr>";
			echo   "<tbody>";
			$aux=0;
			while($row = mysql_fetch_assoc($registros))
			{
				if($aux==0){
					$color="#FFFFFF";
					$aux=1;
				}else{
					$color="#F9F9F9";
					$aux=0;
				}
				echo ("<tr bgcolor='".$color."'>");
				echo "<td><button type='button' class='btn btn-link btn-xs' data-toggle='tooltip' data-placement='top' onclick='editarCliente($row[idclientesventas])' title='Modificar Datos de Cliente'>$row[idclientesventas]</button></td>";
				//echo   "<td>".$row['idclientesventas']."</td>";
				echo   "<td>"._convert($row['nombre'])."</td>";
				echo   "<td>"._convert($row['direccion'])."</td>";
				echo   "<td>".$row['telefono']."</td>";
				echo   "<td>"._convert($row['contacto'])."</td>";
				echo   "<td>".$row['telcontacto']."</td>";
				echo   "<td>"._convert($row['cargo'])."</td>";
				echo   "<td>"._convert($row['email'])."</td>";
				if($_codigo==2)//potenciales
				{
					if($rowp['seleccionar']== 1 && $rowp['actualizar']== 1 && $rowp['abrir']== 1){
						echo   "<td><input type = button class='btn btn-info btn-xs'  value= 'Ver Seguimiento' onclick='ver(". $row['idclientesventas'] .",2)'>";
						echo   "&nbsp;<input type = button class='btn btn-link btn-xs'  value= 'Convertir a Activo' onclick='convertir(". $row['idclientesventas'] .")'></td>";
					}
				}
				else if($_codigo==1)//prospectos
				{
					if($rowp['seleccionar']== 1 && $rowp['actualizar']== 1 && $rowp['abrir']== 1){
						echo   "<td><input type = button class='btn btn-info btn-xs'  value= 'Ver Seguimiento' onclick='ver(". $row['idclientesventas'] .",1)'>";
						echo   "&nbsp;<input type = button class='btn btn-link btn-xs'  value= 'Convertir a Potencial' onclick='convertir2(". $row['idclientesventas'] .")'></td>";
					}
				}
				else if($_codigo==3)//activos
				{
					if($rowp['seleccionar']== 1 && $rowp['actualizar']== 1 && $rowp['abrir']== 1){
						echo   "<td><input type = button class='btn btn-info btn-xs'  data-toggle='tooltip' data-placement='top' title='Ver Detalles del Cliente' value= 'Ver' onclick='ver2(". $row['idclientesventas'] .")'>";
						echo   	"&nbsp;<button type='button' class='btn btn-link btn-xs' title='Registrar Pago del Cliente' onclick='regpago(".$row['idclientesventas'].")'> Registrar Pago</button>";
						echo   	"&nbsp;<button type='button' class='btn btn-link btn-xs' title='Desactivar Cliente' onclick='desactivarCliente(".$row['idclientesventas'].")'> Desactivar</button>";
						echo   	"&nbsp;<button type='button' class='btn btn-link btn-xs' title='Mensajer&iacute;a' onclick='establecermsj(".$row['idclientesventas'].")'> Mensajer&iacute;a</button></td>";
					}
				}
					
				echo   "</tr>";
			}
			echo   "</tbody>";
			echo   "</table>";
			mysql_close();
		}
			
	
	function _convert($content) {
		if(!mb_check_encoding($content, 'UTF-8')
		OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {
	
			$content = mb_convert_encoding($content, 'UTF-8');
	
			if (mb_check_encoding($content, 'UTF-8')) {
				// log('Converted to UTF-8');
			} else {
				// log('Could not converted to UTF-8');
			}
		}
		return $content;
	}
?>
